#include "TraceMultiple.h"
#include "Camera.h"

Color TraceMultiple::TraceRay( const Ray& ray, int depth) const
{
    ContactData data = m_scene->CheckContact(ray);
    //if we are a shadow ray, and we hit something, we are in shadow!
    if (data.m_hit)   
    {
        //this is the color to return
        Color col = data.m_hitTarget->GetColor();
        //this is the light direction
        Vec3 LightDirection(0,-1,0);
        LightDirection.normalise();
        Vec3 normal = data.m_hitNormal;
        normal.normalise();
        // Determine diffuse light intensity that strikes the vertex
        float s = std::max(LightDirection.GetInverted().DotProduct(normal), 0.0f);
        // Compute reflection vector
        Vec3 r =LightDirection.GetInverted().Reflect(normal);
        // Compute the vector from vertex to eye position
        Vec3 toEye = (data.m_hitPoint - m_scene->m_camera->m_pos).normalise();
        // Determine how much specular light makes it to the eye
        float t = pow(std::max(r.DotProduct(toEye), 0.00000001f), 80.0f);
        // Compute ambient, diffuse and specluar terms seperately
        Color spec =  Color().SetWhite() * t;
        // Determine diffuse light intensity that strikes the vertex
        Color diffuse = (col * Color(0.8f,0.8f,0.8f,1.0f)) * s;
        Color ambient = (col * Color(0.2f,0.2f,0.2f,1.0f));
        // Sum of them all together
        return ambient + diffuse + spec;
    }
    else
        return (m_scene->m_clearColor);
}
